import { http } from '@/server'
import type { TeacherDispatchType } from './interface'

/** 获取师资派遣详情数据 */
export const fetchTeacherDispatchDetail = async (id: number) => {
    try {
        const { data } = await http.get(`/country/group/info/${id}`)
        return data
    } catch (error) {
        return undefined
    }
}

/**
 * 培训计划审核 - 通过
 * @param {number} 培训计划id
 */
export const approveTrainPlan = async (id: number) => {
    return await http.post(`/engineer/plan/check/${id}`, {
        check: 1,
    })
}

/**
 * 培训计划审核 - 驳回
 * @param {number} 培训计划id
 * @param {reason} 意见
 */
export const rejectTrainPlan = async (id: number, reason: string) => {
    return await http.post(`/engineer/plan/check/${id}`, {
        check: 2,
        check_reason: reason,
    })
}

/** 取消派遣 */
export interface CancelDispatchFormData {
    /** 班级ID */
    gid: number
    /** 取消类型 （1-班级原因 2-培训师原因） */
    type?: 1 | 2 | string
    /** 培训师UID */
    teachers: number[]
    /** 取消派遣理由 */
    reason?: string
    /** 操作人 */
    operator?: string
}

/** 取消派遣 */
export const cancelDispatch = (gid: number, params: CancelDispatchFormData) => {
    const formData = {
        default_type: '培训师原因',
        operator: '',
        reason: '',
        ...params,
        type: params.type ?? 2,
    }
    http.post(`/country/group/cancel-dispatch/${gid}`, formData)
}

/**
 * 获取可派遣的讲师列表
 * @param gid 班级ID
 * @returns
 */
export const fetchCanDispatchTeachers = (
    gid: number,
    params: { page: number; per_page: number } = { page: 1, per_page: 5 },
) =>
    http.get<TeacherDispatchType.CanDispatchTeacherPageData>(`/country/group/teacher/${gid}`, {
        params: { ...params },
    })

/**
 * 派遣培训师
 * @param id 班级ID
 * @param uid 培训师ID
 * @param params
 * @returns
 */
export const dispatchTeacher = (
    id: number,
    uid: number,
    params: {
        /** 班级ID */
        id: number
        /** 培训师ID */
        uid: number
        /** 培训师编码 */
        code: string
    },
) => http.post(`/country/group/dispatch/${id}/${uid}`, { ...params })
